package dtos

import "github.com/edgexfoundry/edgex-go/internal/app/models"

type ResourceProperties struct {
	// 属性的数据类型
	ValueType string `json:"valueType" yaml:"valueType" validate:"required,edgex-dto-value-type"`
	// 读写
	ReadWrite    string `json:"readWrite" yaml:"readWrite" validate:"required,oneof='R' 'W' 'RW' 'WR'"`
	Units        string `json:"units" yaml:"units"`
	Minimum      string `json:"minimum" yaml:"minimum"`
	Maximum      string `json:"maximum" yaml:"maximum"`
	DefaultValue string `json:"defaultValue" yaml:"defaultValue"`
	Mask         string `json:"mask" yaml:"mask"`
	Shift        string `json:"shift" yaml:"shift"`
	Scale        string `json:"scale" yaml:"scale"`
	Offset       string `json:"offset" yaml:"offset"`
	Base         string `json:"base" yaml:"base"`
	Assertion    string `json:"assertion" yaml:"assertion"`
	MediaType    string `json:"mediaType" yaml:"mediaType"`
}

type ProductResource struct {
	Description string `json:"description" yaml:"description"`
	Name        string `json:"name" yaml:"name" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
	// 产品属性的标识
	Code       string                 `json:"code,omitempty"`
	IsHidden   bool                   `json:"isHidden" yaml:"isHidden"`
	Tag        string                 `json:"tag" yaml:"tag"`
	Properties ResourceProperties     `json:"properties" yaml:"properties"`
	Attributes map[string]interface{} `json:"attributes" yaml:"attributes"`
}

type ProductAttribute struct {
	// 创建和更新时间
	DBTimestamp
	// id
	Id string `json:"id"`
	// 产品id
	ProductId string `json:"productId"`
	// 产品resource
	Resource ProductResource `json:"productResource"`
}

func toResourcePropertiesModel(dtorp ResourceProperties) models.ResourceProperties {
	var rp models.ResourceProperties
	rp.ValueType = dtorp.ValueType
	rp.ReadWrite = dtorp.ReadWrite
	rp.Units = dtorp.Units
	rp.Minimum = dtorp.Minimum
	rp.Maximum = dtorp.Maximum
	rp.DefaultValue = dtorp.DefaultValue
	rp.Mask = dtorp.Mask
	rp.Shift = dtorp.Shift
	rp.Scale = dtorp.Scale
	rp.Offset = dtorp.Offset
	rp.Base = dtorp.Base
	rp.Assertion = dtorp.Assertion
	rp.MediaType = dtorp.MediaType
	return rp
}

func ToProductResourceModel(dtopr ProductResource) models.ProductResource {
	var pr models.ProductResource
	pr.Description = dtopr.Description
	pr.Name = dtopr.Name
	pr.Code = dtopr.Code
	pr.IsHidden = dtopr.IsHidden
	pr.Tag = dtopr.Tag
	pr.Properties = toResourcePropertiesModel(dtopr.Properties)
	pr.Attributes = dtopr.Attributes
	return pr
}

func ToProductAttributeModel(dto ProductAttribute) models.ProductAttribute {
	var pa models.ProductAttribute
	pa.Id = dto.Id
	pa.ProductId = dto.ProductId
	pa.Resource = ToProductResourceModel(dto.Resource)
	pa.Created = dto.Created
	pa.Modified = dto.Modified
	return pa
}

func FromResourcePropertiesModelToDto(rp models.ResourceProperties) ResourceProperties {
	var dtorp ResourceProperties
	dtorp.ValueType = rp.ValueType
	dtorp.ReadWrite = rp.ReadWrite
	dtorp.Units = rp.Units
	dtorp.Minimum = rp.Minimum
	dtorp.Maximum = rp.Maximum
	dtorp.DefaultValue = rp.DefaultValue
	dtorp.Mask = rp.Mask
	dtorp.Shift = rp.Shift
	dtorp.Scale = rp.Scale
	dtorp.Offset = rp.Offset
	dtorp.Base = rp.Base
	dtorp.Assertion = rp.Assertion
	dtorp.MediaType = rp.MediaType
	return dtorp
}

func FromProductResourceModelToDto(pr models.ProductResource) ProductResource {
	var dtopr ProductResource
	dtopr.Description = pr.Description
	dtopr.Name = pr.Name
	dtopr.Code = pr.Code
	dtopr.IsHidden = pr.IsHidden
	dtopr.Tag = pr.Tag
	dtopr.Properties = FromResourcePropertiesModelToDto(pr.Properties)
	dtopr.Attributes = pr.Attributes
	return dtopr
}

func FromProductAttributeModelToDto(pa models.ProductAttribute) ProductAttribute {
	var dto ProductAttribute
	dto.Id = pa.Id
	dto.ProductId = pa.ProductId
	dto.Resource = FromProductResourceModelToDto(pa.Resource)
	dto.Created = pa.Created
	dto.Modified = pa.Modified
	return dto
}
